package com.example.graduatetest.view.model;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.widget.Toast;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.graduatetest.activitys.HomeActivity;
import com.example.graduatetest.bean.Student;
import com.example.graduatetest.callback.LoginCallBack;
import com.example.graduatetest.model.LoginModel;


public class LoginViewModel extends ViewModel {

    private LoginModel model;

    private MutableLiveData<String> number;
    private MutableLiveData<String> password;
    private MutableLiveData<Boolean> isChecked;
    private Application application;
    private Activity activity;
    private Handler handler = new Handler();


    //初始化
    public void init(Activity activity){
        this.application = activity.getApplication();
        this.activity = activity;
        model = new LoginModel();
        checkUser();
        if(number == null) number = new MutableLiveData<>();
        if(password == null) password = new MutableLiveData<>();
        if(isChecked == null) isChecked = new MutableLiveData<>();
        isChecked.setValue(true);
    }

    //确定是否有sharePres
    private void checkUser(){
        SharedPreferences preferences = activity.getSharedPreferences("data", Context.MODE_PRIVATE);
        boolean isLogin = preferences.getBoolean("isLogin", false);
        final boolean isChecked = preferences.getBoolean("isStudent", true);
        final String number = preferences.getString("number", "");
        final String password = preferences.getString("password", "");
        if(isLogin){
            model.login(number, password,isChecked, new LoginCallBack() {
                @Override
                public void onSuccess(String name) {
                    if(!name.equals("fail")){
                        //存储基本信息
                        saveSomeMessage(number, password, isChecked,name);

                        Intent intent = new Intent(application, HomeActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        application.startActivity(intent);
                        activity.finish();
                    }else{
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(application, "账户或者密码错误", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                }
                @Override
                public void onFailed() {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(application, "网络错误或其它错误", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            });
        }

    }

    public void login(){

        //登录
        if(number.getValue() != null && password.getValue() != null && isChecked.getValue() != null){

            model.login(number.getValue(), password.getValue(),isChecked.getValue(), new LoginCallBack() {
                @Override
                public void onSuccess(String name) {
                    if(!name.equals("fail")){
                        //存储基本信息
                        saveSomeMessage(number.getValue(), password.getValue(), isChecked.getValue(),name);

                        Intent intent = new Intent(application, HomeActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        application.startActivity(intent);
                        activity.finish();
                    }else{
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(application, "账户或者密码错误", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                }
                @Override
                public void onFailed() {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(application, "网络错误或其它错误", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            });
        }else{
            Toast.makeText(application, "学号、学校或密码不能为空", Toast.LENGTH_SHORT).show();
        }

    }

    private void saveSomeMessage(String number,String password,boolean isStudent,String name){
        SharedPreferences.Editor editor = application.getSharedPreferences("data", Context.MODE_PRIVATE).edit();
        editor.putString("number", number);
        editor.putString("password", password);
        editor.putBoolean("isStudent", isStudent);
        editor.putString("name", name);
        editor.putBoolean("isLogin", true);
        editor.apply();
    }


    public MutableLiveData<String> getNumber() {
        return number;
    }

    public MutableLiveData<String> getPassword() {
        return password;
    }

    public MutableLiveData<Boolean> getIsChecked() {
        return isChecked;
    }
}
